//
//  ResetPwdViewController.m
//  moon
//
//  Created by beyself on 16/8/3.
//  Copyright © 2016年 beyself. All rights reserved.
//
#import "Utils.h"
#import "ResetPwdViewController.h"

@interface ResetPwdViewController ()

@end

@implementation ResetPwdViewController

- (void) viewDidLoad
{
    [super viewDidLoad];
    self.title = @"设置密码";
    
    for(int i = 0; i < 2; i++)
    {
        UIView* subview = [[UIView alloc] init];
        subview.frame = CGRectMake(0, 20 + 49*i, SCREEN_WIDTH, 48);
        subview.backgroundColor = WHITE;
        
        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, 48, 48);
        button.enabled = NO;
        [button setImage:[UIImage imageNamed:@"icon_code"] forState:UIControlStateDisabled];
        [subview addSubview:button];
        
        UITextField* textfield = [[UITextField alloc] init];
        textfield.tag = 2000 + i;
        textfield.frame = CGRectMake(48, 0, SCREEN_WIDTH - 58, 48);
        textfield.textColor = BLACK;
        textfield.keyboardType = UIKeyboardTypeEmailAddress;
        textfield.secureTextEntry = YES;
        if(i == 0)
            textfield.placeholder = @"请输入您要新设置的密码";
        else
            textfield.placeholder = @"请重复您刚才输入的密码";
        [subview addSubview:textfield];
        [self.view addSubview:subview];
    }
    
    UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTap:)];
    [singleTapGestureRecognizer setNumberOfTapsRequired:1];
    [self.view addGestureRecognizer:singleTapGestureRecognizer];
    
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, 140, SCREEN_WIDTH - 20, 44);
    button.layer.cornerRadius = 22;
    button.layer.masksToBounds = YES;
    button.titleLabel.font = FONT_15;
    [button addTarget:self action:@selector(modifyPwd) forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundImage:[Utils imageFromColor:ORANGE] forState:UIControlStateNormal];
    [button setBackgroundImage:[Utils imageFromColor:LINE] forState:UIControlStateHighlighted];
    [button setTitle:@"完成" forState:UIControlStateNormal];
    [self.view addSubview:button];
}

- (void)singleTap:(UIGestureRecognizer*)gestureRecognizer
{
    UITextField* newPwd0 = ((UITextField*)[self.view viewWithTag:2000]);
    UITextField* newPwd1 = ((UITextField*)[self.view viewWithTag:2001]);
    
    [newPwd0 resignFirstResponder];
    [newPwd1 resignFirstResponder];
}

- (void) modifyPwd
{
    NSString* newPwd0 = ((UITextField*)[self.view viewWithTag:2000]).text;
    NSString* newPwd1 = ((UITextField*)[self.view viewWithTag:2001]).text;
    
    if(newPwd0.length == 0 || newPwd1.length == 0)
    {
        [self showAllTextDialog:@"请完善所有信息"];
        return;
    }
    
    if(![newPwd0 isEqualToString:newPwd1])
    {
        [self showAllTextDialog:@"两次密码不一致"];
        return;
    }
    
    [(UITextField*)[self.view viewWithTag:2001] resignFirstResponder];
    [(UITextField*)[self.view viewWithTag:2000] resignFirstResponder];
    
    NSMutableDictionary* param = [[NSMutableDictionary alloc] init];
    [param setValue:newPwd1 forKey:@"password"];
    [param setValue:_tel forKey:@"telephone"];
    [Utils sendHttpPost:param method:@"update_profile" controller:self flag:0];
}

- (void) httpCallback:(NSDictionary*)data flag:(int)flag
{
    if(flag == 0)
    {
        NSString* newPwd1 = ((UITextField*)[self.view viewWithTag:2001]).text;
        NSMutableDictionary* param = [[NSMutableDictionary alloc] init];
        [param setObject:_tel forKey:@"phonenumber"];
        [param setObject:newPwd1 forKey:@"password"];
        if(APPDELEGATE.deviceToken.length) [param setObject:APPDELEGATE.deviceToken forKey:@"ios_token"];
        [Utils sendHttpPost:param method:@"member_login" controller:self flag:1];
    }
    else
    {
        [Utils saveCustomObject:data[@"member"] key:LOGIN_INFO];
        NSString* pwd = ((UITextField*)[self.view viewWithTag:2001]).text;
        
        [Utils saveDefaultUser:_tel key:@"username"];
        [Utils saveDefaultUser:pwd key:@"password"];
        
        [APPDELEGATE showMainView];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
